package converters;

import java.util.Iterator;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import modelo.Pais;
import modelo.UnidadMedida;
import services.PaisService;

@FacesConverter("paisConverter")
public class PaisConverter implements Converter {
	
	public Object getAsObject(FacesContext fc, UIComponent uic, String value) {
        if(value != null && value.trim().length() > 0) {
            PaisService service = (PaisService) fc.getExternalContext().getApplicationMap().get("paisService");
//            return service.obtenerPaisById(Integer.parseInt(value));
            Iterator<Pais> i=service.getListaPais().iterator();
            while (i.hasNext()) {
				Pais p = (Pais) i.next();
				if(p.getIdPais() == Integer.parseInt(value))
					return p;
			}
            return null;
        }
        else {
            return null;
        }
    }
 
    public String getAsString(FacesContext fc, UIComponent uic, Object object) {
        if(object != null) {
        	return String.valueOf(((Pais) object).getIdPais());
        }
        else {
            return null;
        }
    }  

}